﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz;
using RegulatoryPlatform.DB;
using RegulatoryPlatform.Models;
using RegulatoryPlatform.Tools;

namespace RegulatoryPlatform.ScheduleTask
{
    /// <summary>
    /// 在线复诊记录
    /// </summary>
    public class YL_ZXFZJL : Base
    {
        public override async Task Execute(IJobExecutionContext context)
        {
            string param = DateTime.Now.ToString("yyyyMMdd");
            var json = rDData.GetDataByMethod("Address_Get_Revisit_List", new Revisit_ListRequest
            {
                FZRQ = param
            });

            var response = Serialize<ZXFZJL>(json);

            if (response.status == 10000 && response.msg == "success")
            {
                try
                {
                    foreach (var item in response.content)
                    {
                        TB_YL_ZXFZJL entity = new TB_YL_ZXFZJL();
                        entity.YLJGDM = YLJGDM;
                        entity.FZLSH = item.fzlsh;
                        entity.WSJGDM = WSJGDM;
                        entity.FZSQSJ = DateTime.Parse(item.fzsqsj);

                        entity.FZJZSJ = DateTime.Parse(item.fzjzsj ?? "1991.01.01");
                        entity.HZQDSJ = entity.FZJZSJ;

                        //TODO:诊疗时常暂时填0
                        entity.ZLSC = 0;
                        entity.KLX = "9";
                        entity.SFZJLX = "01";
                        entity.JZKSBM = item.doctorOutId.Split('_')[0];

                        #region 暂时没有值 空着
                        entity.ZDBM = "";
                        entity.ZDBMLX = "";
                        entity.SJYWQMZ = "";
                        entity.YHRYZSXLH = "";
                        entity.KXSJC = "";
                        #endregion

                        entity.KH = item.kh;
                        entity.SFZJHM = item.zjhm;
                        entity.HZXM = item.hzmc;
                        entity.TWYSPDAH = item.twyspdah;
                        entity.PJ = item.pj;

                        entity.XGBZ = item.xgbz;

                        db.Set<TB_YL_ZXFZJL>().Add(entity);
                    }
                    await db.BulkSaveChangesAsync();
                    await Console.Out.WriteLineAsync($"在线复诊记录 数据同步完成 在{DateTime.Now}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"在线复诊记录 出错了:{ex.Message}");
                    JobExecutionException e2 = new JobExecutionException(ex);
                    // 这个工作将立即重新开始  
                    e2.RefireImmediately = true;
                    throw e2;
                }
            }
            else
            {
                await Console.Out.WriteLineAsync($"未能找到 {param} 对应的 在线复诊记录 ");
            }
        }
    }

    /// <summary>
    /// 在线复诊记录 入参
    /// </summary>
    public class Revisit_ListRequest
    {
        /// <summary>
        /// 业务时间 YYYYMMDD
        /// </summary>
        public string FZRQ { get; set; }
    }

}
